package airport.server.service;

import airport.server.DAO.UserDAO;
import airport.shared.beans.UserBean;
import airport.shared.service.AirportService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AirportServiceImpl extends RemoteServiceServlet implements AirportService {

	@Override
	public UserBean connexion(String email, String pw) {
		UserDAO uDAO = new UserDAO();
        UserBean userBean;

        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("UserID", 1);

        userBean = uDAO.connexion(email, pw);
        if (userBean != null) {
            session.setAttribute("nom", userBean.getNom());
            session.setAttribute("prenom", userBean.getPrenom());
        }

        return userBean;


    }

    @Override
    public UserBean checkLogin() {
        UserBean userBean = null;


        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("UserID") == null)
            return userBean;
        else {
            userBean = new UserBean();
            userBean.setPrenom((String) session.getAttribute("prenom"));
            userBean.setNom((String) session.getAttribute("nom"));

            return userBean;
        }
    }

    @Override
    public void logout() {

        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null)
            return;
        // do some logout stuff ...
        session.invalidate();
    }

}